package com.xiaolun.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {
	public static void main(String[] args) throws Exception {
		FileInputStream fileInputStream = new FileInputStream("1.txt");
		FileChannel fileChannel01 = fileInputStream.getChannel();

		FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
		FileChannel fileChannel02 = fileOutputStream.getChannel();

		// 只使用一个byteBuffer进行完成
		ByteBuffer byteBuffer = ByteBuffer.allocate(512);
		while (true) { //循环读取

			//这里有一个重要的操作，一定不要忘了
            /*
             public final Buffer clear() {
                position = 0;
                limit = capacity;
                mark = -1;
                return this;
            }
             */
			byteBuffer.clear(); //清空buffer!! - 否则会循环的出现0
			int read = fileChannel01.read(byteBuffer);
			System.out.println("read =" + read);
			if (read == -1) { //表示读完
				break;
			}
			//将buffer 中的数据写入到 fileChannel02 -- 2.txt
			byteBuffer.flip();
			fileChannel02.write(byteBuffer);
		}

		//关闭相关的流
		fileInputStream.close();
		fileOutputStream.close();
	}
}
